package com.yuezhu.global;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Fragment;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Rect;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;

import com.yuezhu.com_volley.OkRequestQueue;
import com.yuezhu.com_volley.OkVolley;

import org.greenrobot.eventbus.EventBus;

import java.io.File;
import java.io.FileOutputStream;

/**
 * Fragment基类
 * 
 * @创建人：zhaoshuzhen
 * @创建时间：2015/12/15 11:57
 */
@SuppressLint("NewApi")
public abstract class BaseFragment extends Fragment  {
	private final static String WINDOWBITMAP = "screenshots.jpg";//截图文件名
	private File mFileTemp;//截图文件
	protected View backView;
	protected ListView listView;
	protected OkRequestQueue queue;//网络访问类

	/**
	 * 初始化控件
	 * 
	 * @param view
	 */
	public abstract void initview(View view);

	/**
	 * 设置监听
	 */
	public abstract void setLiseners();


	@Override
	public void onResume() {
		super.onResume();
		/*MobclickAgent.onResume(getActivity());
		StatService.onResume(getActivity()) ;*/
	}

	@Override
	public void onPause() {
		super.onPause();
		/*MobclickAgent.onPause(getActivity());
		StatService.onPause(getActivity());*/
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
//		BusProvider.getInstance().unregister(this);
//		EventBus.getDefault().unregister(this);
	}


	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		queue = OkVolley.newOkRequestQueue(getActivity());
		mFileTemp = new File(getActivity().getCacheDir(), WINDOWBITMAP);
//		BusProvider.getInstance().register(this);
//		EventBus.getDefault().register(this);
	}


	/**
	 * 启动视觉差返回Activity
	 * 
	 * @param activity
	 * @param intent
	 */
	public void startParallaxSwipeBackActivty(Activity activity, Intent intent) {
		startParallaxSwipeBackActivty(activity, intent, false);
	}

	public void startForResultParallaxSwipeBackActivty(Activity activity,
			Intent intent) {
		startForResultParallaxSwipeBackActivty(activity, intent, false);
	}

	private final int REQUESTCODE = 1;

	public void startForResultParallaxSwipeBackActivty(Activity activity,
													   Intent intent, boolean isFullScreen) {

		startActivityForResult(intent, REQUESTCODE);
		screenshots(activity, isFullScreen);
		// this.overridePendingTransition(R.anim.slide_in_right,
		// R.anim.slide_out_right);
	}

	/**
	 * startParallaxSwipeBackActivty
	 * 
	 * @param activity
	 * @param intent
	 * @param isFullScreen
	 */
	public void startParallaxSwipeBackActivty(Activity activity, Intent intent,
											  boolean isFullScreen) {

		startActivity(intent);
		screenshots(activity, isFullScreen);
		// this.overridePendingTransition(R.anim.slide_in_right,
		// R.anim.slide_out_right);
	}

	/**
	 * this screeshots form
	 * 
	 * @param activity
	 * @param isFullScreen
	 */
	public void screenshots(Activity activity, boolean isFullScreen) {
		try {
			// View是你需要截图的View
			View decorView = activity.getWindow().getDecorView();
			decorView.setDrawingCacheEnabled(true);
			decorView.buildDrawingCache();
			Bitmap b1 = decorView.getDrawingCache();
			// 获取状态栏高度 /
			Rect frame = new Rect();
			activity.getWindow().getDecorView()
					.getWindowVisibleDisplayFrame(frame);
			int statusBarHeight = frame.top;
			// 获取屏幕长和高 Get screen width and height
			int width = activity.getWindowManager().getDefaultDisplay()
					.getWidth();
			int height = activity.getWindowManager().getDefaultDisplay()
					.getHeight();
			// 去掉标题栏 Remove the statusBar Height
			Bitmap bitmap;
			if (isFullScreen) {
				bitmap = Bitmap.createBitmap(b1, 0, 0, width, height);
			} else {
				bitmap = Bitmap.createBitmap(b1, 0, statusBarHeight, width,
						height - statusBarHeight);
			}
			decorView.destroyDrawingCache();
			FileOutputStream out = new FileOutputStream(mFileTemp);
			bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
